package com.chuangshixinchen.enterprise.common;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Component
public class DateUtil {

    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }


    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取每天的开始时间 00:00:00:00
     *
     * @param date
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        return dateStart.getTime();
    }

    /**
     * 获取每天的上班时间 08:00:00:00
     *
     * @param date
     * @return
     */
    public static Date getWorkTime(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 8);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        return dateStart.getTime();
    }

    /**
     * 获取每天的下班时间 17:00:00:00
     *
     * @param date
     * @return
     */
    public static Date getOffTime(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 17);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        return dateStart.getTime();
    }

    /**
     * 获取每天的结束时间 23:59:59:999
     *
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        return dateEnd.getTime();
    }

    //日期格式转时间
    public static String timeToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");//日期格式
        return simpleDateFormat.format(date);
    }

    //日期格式转时间
    public static String timeToString(Date date,String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);//日期格式
        return simpleDateFormat.format(date);
    }

    //日期格式转时间
    public static String dateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//日期格式
        return simpleDateFormat.format(date);
    }

    //String 转Date
    public static Date stringToTime(String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    //String 转Date
    public static Date stringToTime(String dateString,String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);//注意月份是MM
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getDifferentDays(Date dateSmall,Date dateLarge)
    {
        int days = (int) ((dateLarge.getTime() - dateSmall.getTime()) / (1000*3600*24));
        return days;
    }

    public static long getDifferentTimestamp(Date dateSmall,Date dateLarge)
    {
        return dateLarge.getTime() - dateSmall.getTime();
    }


    /**
     * 获取某个月第一天的开始时刻
     *
     * @param month
     * @return
     */
    public static Date getFirstDayTimeOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期，获取开始时刻
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String firstDayOfMonth = sdf.format(cal.getTime()) + " 00:00:00";
        return cal.getTime();
    }

    /**
     * 获得某月的最后一天的最后时刻
     *
     * @param month 要获取的月份
     * @return
     */
    public static Date getLastDayTimeOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份+1
        cal.set(Calendar.MONTH, month);
        // 获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        // 减1毫秒
        cal.add(Calendar.MILLISECOND, -1);
        // 格式化日期，获取最后时刻
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String lastDayOfMonth = sdf.format(cal.getTime()) + " 23:59:59";
        return cal.getTime();
    }


    /**
     * 根据年 月 获取对应的月份 天数
     */

    public static int getDaysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        int maxDate = calendar.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 某个时间点上加减时间戳，后返回时间Date
     */
    public static Date addDate(Date date, long timeStamp) {
        long timeDate = date.getTime();
        long timeResult = timeDate + timeStamp;
        return new Date(timeResult);
    }

    /**
     * 某个时间点上加减年月日，后返回时间Date
     */
    public static Date addYMD(Date date, int year, int month, int day) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.YEAR, year);//这里就是月份的相加
        cl.add(Calendar.MONTH, month);//这里就是月份的相加
        cl.add(Calendar.DAY_OF_MONTH, day);//这里就是月份的相加
        return cl.getTime();//获取到相加后的时间
    }
}
